package com.jdcjk.project.car.presenter;

import android.content.Context;

import com.google.gson.Gson;
import com.jdcjk.base.presenter.BasePresenter;
import com.jdcjk.project.account.model.entity.Result;
import com.jdcjk.project.car.model.CarModel;
import com.jdcjk.project.car.model.entity.LocationReplayResult;
import com.jdcjk.project.car.model.entity.SosNumberResult;
import com.jdcjk.project.car.model.entity.TerminalInfoListResult;
import com.jdcjk.project.car.model.entity.TerminalInfoResult;
import com.zhy.http.okhttp.callback.StringCallback;

import okhttp3.Call;
import okhttp3.Request;

/**
 * Created by dong on 2016/12/1.
 * 轨迹回放中介类
 */

public class CarPresenter extends BasePresenter<CarModel> {

    public CarPresenter(Context context) {
        super(context);
    }

    @Override
    public CarModel bindModel() {
        return new CarModel(getContext());
    }


    public void setSelected(String url, String selected, final OnUIThreadListener<String> onUIThreadListener) {
    }

    /**
     * 获取设备报警信息
     *
     * @param url                请求url
     * @param terminalId         设备id
     * @param onUIThreadListener onUIThreadListener
     */
    public void getCarWarmingInfo(String url, String terminalId, final OnUIThreadListener<TerminalInfoListResult> onUIThreadListener) {
        getModel().getCarWarmingInfo(url, terminalId, new StringCallback() {
            @Override
            public void onBefore(Request request, int id) {
                onUIThreadListener.onBefore(request, id);
            }

            @Override
            public void onAfter(int id) {
                onUIThreadListener.onAfter(id);
            }

            @Override
            public void onError(Call call, Exception e, int i) {
                onUIThreadListener.onError(call, e, i);
            }

            @Override
            public void onResponse(String s, int i) {
                Gson gson = new Gson();
                TerminalInfoListResult terminalInfoListResult = gson.fromJson(s, TerminalInfoListResult.class);

                onUIThreadListener.onResponse(terminalInfoListResult);
            }
        });
    }

    /**
     * 删除一条报警信息
     *
     * @param url                请求url
     * @param id                 信息id
     * @param onUIThreadListener onUIThreadListener
     */
    public void deleteWarningInfo(String url, String id, final OnUIThreadListener<Result> onUIThreadListener) {
        getModel().deleteWarningInfo(url, id, new StringCallback() {
            @Override
            public void onBefore(Request request, int id) {
                onUIThreadListener.onBefore(request, id);
            }

            @Override
            public void onAfter(int id) {
                onUIThreadListener.onAfter(id);
            }

            @Override
            public void onError(Call call, Exception e, int i) {
                onUIThreadListener.onError(call, e, i);
            }

            @Override
            public void onResponse(String s, int i) {
                Gson gson = new Gson();
                Result result = gson.fromJson(s, Result.class);
                onUIThreadListener.onResponse(result);
            }
        });
    }

    /**
     * 根据时间获得路径
     *
     * @param url                请求url
     * @param terminalID         终端id
     * @param startTime          开始时间
     * @param endTime            结束时间
     * @param onUIThreadListener 在UI线程的回调
     */
    public void queryLocationReplay(String url, String terminalID, String startTime, String endTime, final OnUIThreadListener<LocationReplayResult> onUIThreadListener) {
        getModel().queryLocationReplay(url, terminalID, startTime, endTime, new StringCallback() {
            @Override
            public void onBefore(Request request, int id) {
                onUIThreadListener.onBefore(request, id);
            }

            @Override
            public void onAfter(int id) {
                onUIThreadListener.onAfter(id);
            }

            @Override
            public void onError(Call call, Exception e, int i) {
                onUIThreadListener.onError(call, e, i);
            }

            @Override
            public void onResponse(String s, int i) {
                Gson json = new Gson();
                LocationReplayResult locationReplayResult = json.fromJson(s, LocationReplayResult.class);
                onUIThreadListener.onResponse(locationReplayResult);
            }
        });
    }

    /**
     * 获取单个终端信息
     *
     * @param url                terminalId
     * @param terminalId         终端id
     * @param onUIThreadListener 在UI线程的回调
     */
    public void getTerminal(String url, String terminalId, final OnUIThreadListener<TerminalInfoResult> onUIThreadListener) {
        getModel().getTerminal(url, terminalId, new StringCallback() {
            @Override
            public void onBefore(Request request, int id) {
                onUIThreadListener.onBefore(request, id);
            }

            @Override
            public void onAfter(int id) {
                onUIThreadListener.onAfter(id);
            }

            @Override
            public void onError(Call call, Exception e, int i) {
                onUIThreadListener.onError(call, e, i);
            }

            @Override
            public void onResponse(String s, int i) {
                Gson json = new Gson();
                TerminalInfoResult terminalInfoResult = json.fromJson(s, TerminalInfoResult.class);
                onUIThreadListener.onResponse(terminalInfoResult);
            }
        });
    }

    /**
     * 修改设备信息
     *
     * @param url                请求url
     * @param id                 设备id
     * @param displayid          设备名称
     * @param carId              车牌号码
     * @param cardNum            设备手机卡号
     * @param linkman            设备联系人
     * @param linkphone          设备联系人手机号
     * @param onUIThreadListener 在UI线程的回调
     */
    public void updataTerminalInfo(String url, String id, String displayid, String carId, String cardNum, String linkman, String linkphone, final OnUIThreadListener<Result> onUIThreadListener) {
        getModel().updataTerminalInfo(url, id, displayid, carId, cardNum, linkman, linkphone, new StringCallback() {
            @Override
            public void onBefore(Request request, int id) {
                onUIThreadListener.onBefore(request, id);
            }

            @Override
            public void onAfter(int id) {
                onUIThreadListener.onAfter(id);
            }

            @Override
            public void onError(Call call, Exception e, int i) {
                onUIThreadListener.onError(call, e, i);
            }

            @Override
            public void onResponse(String s, int i) {
                Gson gson = new Gson();
                Result result = gson.fromJson(s, Result.class);
                onUIThreadListener.onResponse(result);
            }
        });
    }

    /**
     * 终端重启指令下发
     *
     * @param url                请求url
     * @param terminalId         终端id
     * @param onUIThreadListener 在UI线程的回调
     */
    public void resetTerminal(String url, String terminalId, final OnUIThreadListener<Result> onUIThreadListener) {
        getModel().resetTerminal(url, terminalId, new StringCallback() {
            @Override
            public void onBefore(Request request, int id) {
                onUIThreadListener.onBefore(request, id);
            }

            @Override
            public void onAfter(int id) {
                onUIThreadListener.onAfter(id);
            }

            @Override
            public void onError(Call call, Exception e, int i) {
                onUIThreadListener.onError(call, e, i);
            }

            @Override
            public void onResponse(String s, int i) {
                Gson json = new Gson();
                Result result = json.fromJson(s, Result.class);
                onUIThreadListener.onResponse(result);
            }
        });
    }

    /**
     * 终端重启指令下发
     *
     * @param url                请求url
     * @param terminalId         终端id
     * @param onUIThreadListener 在UI线程的回调
     */
    public void checkLocation(String url, String terminalId, final OnUIThreadListener<Result> onUIThreadListener) {
        getModel().checkLocation(url, terminalId, new StringCallback() {
            @Override
            public void onBefore(Request request, int id) {
                onUIThreadListener.onBefore(request, id);
            }

            @Override
            public void onAfter(int id) {
                onUIThreadListener.onAfter(id);
            }

            @Override
            public void onError(Call call, Exception e, int i) {
                onUIThreadListener.onError(call, e, i);
            }

            @Override
            public void onResponse(String s, int i) {
                Gson json = new Gson();
                Result result = json.fromJson(s, Result.class);
                onUIThreadListener.onResponse(result);
            }
        });
    }

    /**
     * 设置定位上报时间
     *
     * @param url                请求url
     * @param terminalId         设备id
     * @param time               上报时间(单位为s)
     * @param onUIThreadListener 在UI线程的回调
     */
    public void setLocationTime(String url, String terminalId, String time, final OnUIThreadListener<Result> onUIThreadListener) {
        getModel().setLocationTime(url, terminalId, time, new StringCallback() {
            @Override
            public void onBefore(Request request, int id) {
                onUIThreadListener.onBefore(request, id);
            }

            @Override
            public void onAfter(int id) {
                onUIThreadListener.onAfter(id);
            }

            @Override
            public void onError(Call call, Exception e, int i) {
                onUIThreadListener.onError(call, e, i);
            }

            @Override
            public void onResponse(String s, int i) {
                Gson json = new Gson();
                Result result = json.fromJson(s, Result.class);
                onUIThreadListener.onResponse(result);
            }
        });
    }

    /**
     * 设置超速报警速度值
     *
     * @param url                请求url
     * @param terminalId         设备id
     * @param speed              超速速度值(单位km/h)
     * @param onUIThreadListener 在UI线程的回调
     */
    public void setSpeedWarning(String url, String terminalId, String speed, final OnUIThreadListener<Result> onUIThreadListener) {
        getModel().setSpeedWarning(url, terminalId, speed, new StringCallback() {
            @Override
            public void onBefore(Request request, int id) {
                onUIThreadListener.onBefore(request, id);
            }

            @Override
            public void onAfter(int id) {
                onUIThreadListener.onAfter(id);
            }

            @Override
            public void onError(Call call, Exception e, int i) {
                onUIThreadListener.onError(call, e, i);
            }

            @Override
            public void onResponse(String s, int i) {
                Gson json = new Gson();
                Result result = json.fromJson(s, Result.class);
                onUIThreadListener.onResponse(result);
            }
        });
    }

    /**
     * 获取用户下的sos号码
     *
     * @param url                请求url
     * @param userId             用户id
     * @param onUIThreadListener 在UI线程的回调
     */
    public void getTerminalFamily(String url, String userId, final OnUIThreadListener<SosNumberResult> onUIThreadListener) {
        getModel().getTerminalFamily(url, userId, new StringCallback() {
            @Override
            public void onBefore(Request request, int id) {
                onUIThreadListener.onBefore(request, id);
            }

            @Override
            public void onAfter(int id) {
                onUIThreadListener.onAfter(id);
            }

            @Override
            public void onError(Call call, Exception e, int i) {
                onUIThreadListener.onError(call, e, i);
            }

            @Override
            public void onResponse(String s, int i) {
                Gson json = new Gson();
                SosNumberResult sosNumberResult = json.fromJson(s, SosNumberResult.class);
                onUIThreadListener.onResponse(sosNumberResult);
            }
        });
    }

    /**
     * 获取终端未读信息数量
     *
     * @param url                请求UrL
     * @param terminalID         终端id
     * @param onUIThreadListener 回调
     */
    public void getUnreadMsgNumForTerminalId(String url, String terminalID, final OnUIThreadListener<String> onUIThreadListener) {
        getModel().getUnreadMsgNumForTerminalId(url, terminalID, new StringCallback() {
            @Override
            public void onBefore(Request request, int id) {
                onUIThreadListener.onBefore(request, id);
            }

            @Override
            public void onAfter(int id) {
                onUIThreadListener.onAfter(id);
            }

            @Override
            public void onError(Call call, Exception e, int i) {
                onUIThreadListener.onError(call, e, i);
            }


            @Override
            public void onResponse(String s, int i) {
                onUIThreadListener.onResponse(s);
            }
        });
    }

    /**
     * 修改报警信息状态(将该设备所有报警信息都改为已读)
     *
     * @param url                请求UrL
     * @param terminalIDs         终端id
     * @param onUIThreadListener 在UI线程的回调
     */
    public void updateMsgStatus(String url, String terminalIDs, final OnUIThreadListener<Result> onUIThreadListener) {
        getModel().updateMsgStatus(url, terminalIDs, new StringCallback() {
            @Override
            public void onBefore(Request request, int id) {
                onUIThreadListener.onBefore(request, id);
            }

            @Override
            public void onAfter(int id) {
                onUIThreadListener.onAfter(id);
            }

            @Override
            public void onError(Call call, Exception e, int i) {
                onUIThreadListener.onError(call, e, i);
            }

            @Override
            public void onResponse(String s, int i) {
                Gson json = new Gson();
                Result result = json.fromJson(s, Result.class);
                onUIThreadListener.onResponse(result);
            }
        });
    }

    /**
     * 保存sos号码
     *
     * @param url                请求url
     * @param userId             用户id
     * @param mobs               手机号码，多个请用“,”隔开
     * @param informType         提示方式
     * @param onUIThreadListener 在UI线程的回调
     */
    public void saveTermailFamily(String url, String userId, String mobs, String informType, final OnUIThreadListener<Result> onUIThreadListener) {
        getModel().saveTermailFamily(url, userId, mobs, informType, new StringCallback() {
            @Override
            public void onBefore(Request request, int id) {
                onUIThreadListener.onBefore(request, id);
            }

            @Override
            public void onAfter(int id) {
                onUIThreadListener.onAfter(id);
            }

            @Override
            public void onError(Call call, Exception e, int i) {
                onUIThreadListener.onError(call, e, i);
            }

            @Override
            public void onResponse(String s, int i) {
                Gson json = new Gson();
                Result result = json.fromJson(s, Result.class);
                onUIThreadListener.onResponse(result);
            }
        });
    }
}
